package com.gdssoft.pdaweb.action;

/*
 *  捷达世软件（深圳）有限公司
 *  Copyright (C) 2008-2010 ShenZhen JieDaShi Software Limited Company.
 */
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import com.gdssoft.core.command.QueryFilter;
import com.gdssoft.core.util.ContextUtil;
import com.gdssoft.core.util.DateUtil;
import com.gdssoft.core.web.action.BaseAction;
import com.gdssoft.framework.service.system.DepartmentService;
import com.gdssoft.framework.model.system.AppUser;
import com.gdssoft.pdaweb.model.PdaLog;
import com.gdssoft.pdaweb.model.PdaMdAssetInfo;
import com.gdssoft.pdaweb.model.am.PdaAmMeter;
import com.gdssoft.pdaweb.service.PdaLogService;
import com.gdssoft.pdaweb.service.PdaMdAssetInfoService;

import flexjson.JSONSerializer;

public class SupplyAction extends BaseAction {
	@Resource
	private PdaMdAssetInfoService pdaMdAssetInfoService;
	@Resource
	private DepartmentService departmentService;
	@Resource
	private PdaLogService pdaLogService;
	
	private PdaMdAssetInfo asset;

	private PdaAmMeter am;

	private int meterType;//1=电；2=水

	/**
	 * 显示电表资产列表,返回当前用户下属的管理处对应的资产的列表
	 * 关系：siteId-->department.code
	 */
	public String list() {
		String codes = departmentService.getSubDepCodes();
		if(codes.isEmpty()){
			setJsonString("{success:false}");
		}else{
			QueryFilter filter = new QueryFilter(getRequest());
			filter.addFilter("Q_isSupply_S_EQ", "N");// 只查未同步部分
			filter.addFilter("Q_siteId_S_SIN", codes);
			filter.addFilter("Q_classStructureId_S_GT", "0");//只查询水电表信息
			List<PdaMdAssetInfo> list = pdaMdAssetInfoService.getAll(filter);
			
			JSONSerializer serializer = new JSONSerializer();
			StringBuffer buff = new StringBuffer("{success:true,'totalCounts':")
					.append(filter.getPagingBean().getTotalItems()).append(
							",result:");
			buff.append(serializer.exclude(new String[] { "class" })
					.serialize(list));
			buff.append("}");

			jsonString = buff.toString();
		}
		

		return SUCCESS;
	}

	public String save() {
		boolean res = true;
		String msg = "";
		if (meterType == 0) {
			res = false;
			msg = "请选择表类型";
		} else {
			try{
				pdaMdAssetInfoService.saveSupply(meterType,am,asset);
				//保存操作日志
				AppUser appUser = ContextUtil.getCurrentUser();
				String dateT = DateUtil.dateFormat(new Date(), "yyyy-MM-dd");
				PdaLog pdaLog=new PdaLog();
				pdaLog.setAction(appUser.getUsername()+"于"+dateT+"对资产条码"+asset.getBarCode()+"进行资产保存");
				pdaLog.setLogType((short) (meterType-1));
				pdaLog.setUserId(appUser.getUsername());
				pdaLog.setOperateTime(new Date());
				try {
					pdaLogService.save(pdaLog);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					msg = e.getMessage().startsWith("--")?e.getMessage():"--资产Log条码"+asset.getBarCode()+"保存失败";
				}			
			}catch(Exception e){
				res = false;
				msg = e.getMessage().startsWith("--")?e.getMessage():"--保存失败";
				logger.error(msg);
			}
		}
		setJsonString("{success:" + res + ",msg:'"+msg+"'}");
		return SUCCESS;
	}

	public String isExist() {
		boolean res = true;
		boolean isExist = false;
		if (meterType != 0 && !am.getAmId().isEmpty()) {	
			isExist = pdaMdAssetInfoService.isExist(meterType,am.getAmId());
		} else {
			res = false;
		}
		setJsonString("{success:'" + res + "',isExist:'"+isExist+"'}");
		return SUCCESS;
	}

	/**
	 * @return the asset
	 */
	public PdaMdAssetInfo getAsset() {
		return asset;
	}

	/**
	 * @param asset
	 *            the asset to set
	 */
	public void setAsset(PdaMdAssetInfo asset) {
		this.asset = asset;
	}

	/**
	 * @return the am
	 */
	public PdaAmMeter getAm() {
		return am;
	}

	/**
	 * @param am
	 *            the am to set
	 */
	public void setAm(PdaAmMeter am) {
		this.am = am;
	}

	/**
	 * @return the meterType
	 */
	public int getMeterType() {
		return meterType;
	}

	/**
	 * @param meterType
	 *            the meterType to set
	 */
	public void setMeterType(int meterType) {
		this.meterType = meterType;
	}
}
